package durak.client.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import durak.client.control.ClientPlayerControl;
import durak.client.control.ClientSessionControl;

public class LoginToDurak extends JFrame implements MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DurakPanel contentPane;
	private JTextField playerName;
	private JTextField serverAddress;
	public String name;
	public InetAddress address;
	public int portNumber;

	/**
	 * Create the frame.
	 */
	public LoginToDurak() {

		setTitle("Durak Online");
		// setIconImage(Toolkit.getDefaultToolkit().getImage(LoginToDurak.class.getResource("/durak/image/resources/1c3a7eb69a9aff84e6887e1213f6df23.jpg")));
		URL fileLocation = LoginToDurak.class.getResource("/durak/resources/images/DURAK_ICON.png");
		setIconImage(new ImageIcon(fileLocation).getImage());
		setBackground(Color.RED);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 453, 250);
		contentPane = new DurakPanel();
		contentPane.setBackground(SystemColor.activeCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblWelcomeToDurak = new JLabel("WELCOME TO DURAK ONLINE");
		lblWelcomeToDurak.setBounds(155, 18, 185, 38);
		lblWelcomeToDurak.setForeground(SystemColor.activeCaptionText);
		lblWelcomeToDurak.setHorizontalAlignment(SwingConstants.CENTER);

		JLabel enterIpAddLabel = new JLabel("Enter IP address of Server here -->");
		enterIpAddLabel.setFont(new Font("Calibri", Font.BOLD, 12));
		enterIpAddLabel.setBackground(UIManager.getColor("text"));
		enterIpAddLabel.setBounds(15, 126, 215, 36);

		JLabel labelEnterYouName = new JLabel("Enter your name here -->");
		labelEnterYouName.setFont(new Font("Calibri", Font.BOLD, 12));
		labelEnterYouName.setBackground(UIManager.getColor("text"));
		labelEnterYouName.setBounds(15, 63, 172, 36);

		playerName = new JTextField(); // read the player's name
		playerName.setFont(new Font("Arial", Font.PLAIN, 12));

		playerName.setBackground(Color.WHITE);
		playerName.setBounds(242, 69, 172, 25);
		playerName.setColumns(10);

		serverAddress = new JTextField(); // read IP address
		serverAddress.setFont(new Font("Arial", Font.PLAIN, 12));
		serverAddress.setText("127.0.0.1");

		serverAddress.setBackground(Color.WHITE);
		serverAddress.setBounds(242, 134, 172, 25);
		serverAddress.setColumns(10);

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(5, 5, 447, 21);

		JMenu mnFile = new JMenu("File");
		mnFile.setIcon(null);
		menuBar.add(mnFile);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				System.exit(0);
			}
		});

		mnFile.add(mntmExit);

		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);

		JMenuItem mntmAboutDurak = new JMenuItem("About Durak");
		mntmAboutDurak.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				About aboutScreen = new About();
				aboutScreen.setVisible(true);
			}
		});

		mnAbout.add(mntmAboutDurak);
		contentPane.setLayout(null);
		contentPane.add(lblWelcomeToDurak);
		contentPane.add(enterIpAddLabel);
		contentPane.add(labelEnterYouName);
		contentPane.add(playerName);
		contentPane.add(serverAddress);
		contentPane.add(menuBar);

		JButton btnConnect = new JButton("connect");
		btnConnect.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				// we read the input from the user
				try {
					address = InetAddress.getByName(serverAddress.getText());
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				portNumber = 1234;

				ClientPlayerControl playerControl = new ClientPlayerControl();
				try {
					ClientSessionControl serverSession = new ClientSessionControl(address, portNumber, playerName.getText(), playerControl);
					playerControl.setClientSessionControl(serverSession);
					new Thread(serverSession).start();

				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				dispose();
			}

		});

		btnConnect.setBounds(170, 188, 89, 23);
		contentPane.add(btnConnect);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
